package com.wr.controllers;

import com.wr.pojos.*;
import com.wr.services.PermissionService;
import com.wr.services.RoleService;
import com.wr.services.UserService;
import com.wr.utils.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private RoleService roleService;

    @Resource
    private PermissionService permissionService;

    @RequestMapping("/userList")
    public String userList(PageInfo pageInfo, Model model){
        List<UserEntity> userList = userService.selectUserList(pageInfo);
        model.addAttribute("list",userList);
        return "layui/userManage/user_list";
    }

    /**
     * 初始化修改用户角色的下拉列表
     * @param userId
     * @return
     */
    @RequestMapping(value = "/initRoleName",produces = "text/html; charset=utf-8")
    @ResponseBody
    public String initRoleName(String userId,int roleId){
        List<RoleEntity> roleEntities = roleService.selectRolesByUserId(userId);
        RoleEntity role = roleEntities.get(0);
        List<RoleEntity> RoleList = roleService.selectRoles();
        int RoleListLength = RoleList.size();
        StringBuffer json_roleList = new StringBuffer();
        StringBuffer json_html = new StringBuffer();
        json_roleList.append("{");
        int r_id;
        if(roleId!=0){
            r_id = roleId;
        }else{
            r_id = role.getId();
        }

        for (int i=0;i<RoleListLength;i++) {
            RoleEntity temp = RoleList.get(i);
            int role_id = temp.getId();
            if(role_id==r_id){
                json_roleList.append("\"" + temp.getDescription()+"\":{id:\"" + temp.getId()+"\"}");
                json_html.append("<option id=" + role_id + " selected='selected' value='"+temp.getId()+"'>"+temp.getDescription() + "</option>");
            }else{
                json_roleList.append("\"" + temp.getDescription()+"\":{id:\"" + temp.getId()+"\"}");
                json_html.append("<option id=" + role_id + " value='"+temp.getId()+"'>"+temp.getDescription() + "</option>");
            }
        }
        json_roleList.append("}");
        return json_html.toString();
    }

    /**
     * 展开修改用户角色的弹出层
     * @return
     */
    @RequestMapping("/showEditRolePage")
    public String showEditRolePage(String userId,Model model,int roleId){
        List<PermissionEntity> lists = permissionService.selectPermissionList();
        List<RolePermissionEntity> rolePermissions;
        if(roleId==0){
            List<RoleEntity> roleEntities = roleService.selectRolesByUserId(userId);
            RoleEntity role = roleEntities.get(0);
            rolePermissions = permissionService.selectRolePermissions(role.getId());
        }else{
            rolePermissions = permissionService.selectRolePermissions(roleId);
        }
        List list = new ArrayList();
        for(int i=0;i<rolePermissions.size();i++){
            list.add(rolePermissions.get(i).getPermissionId());
        }
        model.addAttribute("userId",userId);
        model.addAttribute("roleId",roleId);
        model.addAttribute("rolePermissions",rolePermissions);
        model.addAttribute("lists",lists);
        model.addAttribute("rolePermissionstr",list);

        return "layui/userManage/editRole_popup";
    }

    /**
     * 保存修改后的用户
     * @param userId
     * @param roleId
     * @return
     */
    @RequestMapping("/editUserRole")
    @ResponseBody
    public Map<String,Object> editUserRole(String userId,int roleId){
        Map<String,Object> map = new HashMap<>();
        Subject subject = SecurityUtils.getSubject();
        UserEntity userEntity = (UserEntity) subject.getPrincipal();
        if(!"1".equals(userEntity.getId())){
            map.put("result","failed");
            return map;
        }
        RoleUserEntity roleUserEntity = userService.selectByUserAndRole(userId);
        roleUserEntity.setRoleId(roleId);
        userService.updateUserRole(roleUserEntity);
        map.put("result","success");
        return map;
    }
}
